<template>
    <div>
        <div class="page-title">添加管理</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="管理账号.">
                <el-input ref="username" v-model="form.username" placeholder="请输入管理账号"></el-input>
            </el-form-item>

            <el-form-item label="管理密码.">
                <el-input ref="password" v-model="form.password" placeholder="请输入管理密码" show-password></el-input>
            </el-form-item>

            <el-form-item label="管理昵称.">
                <el-input ref="nickname" v-model="form.nickname" placeholder="请输入管理昵称"></el-input>
            </el-form-item>
            <el-form-item label="允许添加门店">
              <el-input ref="allow_store" v-model="form.allow_store" placeholder="允许添加门店" type="number"></el-input>
            </el-form-item>
            <el-form-item label="允许添加工位">
              <el-input ref="allow_station" v-model="form.allow_station" placeholder="允许添加工位" type="number"></el-input>
            </el-form-item>
          <el-form-item label="mch_id" prop="mch_id"
                        :rules="[
                        { required: true, message: '请输入mch_id', trigger: 'blur' },
                        ]">
            <el-input ref="mch_id" v-model="form.mch_id" placeholder="mch_id" type="number"></el-input>
          </el-form-item>
          <el-form-item label="mch_key" prop="mch_key"
                        :rules="[
                        { required: true, message: '请输入mch_key', trigger: 'blur' },
                        ]">
            <el-input ref="mch_key" v-model="form.mch_key" placeholder="mch_key" type="text"></el-input>
          </el-form-item>
            <!--<el-form-item label="分会设置.">
                <el-select v-model="form.branch">
                <el-option label="查看所有分会数据" value=""></el-option>
                <el-option
                    v-for="item in branchs"
                    :key="item.name"
                    :label="item.name"
                    :value="item.name"
                >
                </el-option>
                </el-select>
            </el-form-item>-->

            <el-form-item label="管理状态.">
                <el-switch v-model="form.status" active-color="#f8c15c"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit('form')">添加</el-button>
                <el-button icon="el-icon-close" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        request_manger_create,
        request_getbranchs
    } from '@/util/api'

    export default {
        name: 'AddManager',
        data() {
            return {
                form: {
                    cid: sessionStorage.getItem('cid'),
                    username: '',
                    password: '',
                    nickname: '',
                    status: false,
                    branch: '无',
                  allow_store:0,
                  allow_station:0
                },
                branchs: []
            }
        },
        mounted() {
            this.form.status = true
            request_getbranchs().then((res) => {
                const result = res.data;
                if (result.code == 200) {
                    this.branchs = result.data;
                }
            })
        },
        methods: {
            // 提交
            submit(formName) {

                if (this.form.username == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入管理账号',
                        offset: 70
                    })
                    this.$refs['username'].focus()
                    return false
                }

                if (this.form.password == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入管理密码',
                        offset: 70
                    })
                    this.$refs['password'].focus()
                    return false
                }

                if (this.form.nickname == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入管理昵称',
                        offset: 70
                    })
                    this.$refs['nickname'].focus()
                    return false
                }

              if (this.form.allow_store == 0) {
                this.$notify.info({
                  title: '系统提示',
                  message: '请输入要添加的门店数',
                  offset: 70
                })
                this.$refs['allow_store'].focus()
                return false
              }

              if (this.form.allow_station == 0) {
                this.$notify.info({
                  title: '系统提示',
                  message: '请输入要添加的工位数',
                  offset: 70
                })
                this.$refs['allow_station'].focus()
                return false
              }
              this.$refs[formName].validate((valid) => {
                console.log('dfasdfsdaf',valid);
                if (valid){
                  request_manger_create(this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                      this.$notify.success({
                        title: '系统提示',
                        message: '管理添加成功',
                        offset: 70
                      })
                      this.$router.push('/manager')
                    }
                  }).catch()
                }else{
                  console.log('error submit!!');
                  return false
                }
              })

            },
            // 取消
            cancel() {
                this.$router.push('/manager')
            }
        }
    }
</script>

<style>
</style>